package com.tjetc.view.adminview;

import com.tjetc.proxy.CilentProxy;
import com.zhiyun.domain.Admin;
import com.zhiyun.service.AdminService;
import com.zhiyun.vo.AppData;
import com.zhiyun.vo.Result;

import java.util.List;
import java.util.Scanner;

public class Admin_Manage {
    AdminService adminService= CilentProxy.getProxyInstance(AdminService.class);
    Scanner scanner=new Scanner(System.in);
    public void start() {
        while (true){
            System.out.println("1.查询管理员");
            System.out.println("2.增加管理员");
            System.out.println("3.删除管理员");
            System.out.println("4.管理员权限修改");
            System.out.println("0.返回上一级");
            int i = scanner.nextInt();
            switch (i){
                case 1:
                    allAdmin();
                    break;
                case 2:
                    insertAdmin();
                    break;
                case 3:
                    deleteAdmin();
                    break;
                case 4:
                    updateAdmin();
                    break;
                case 0:
                    return;
            }
        }
    }

    private void allAdmin() {
        while (true){
            Result result = adminService.selectAll();
            if (result.getCode()==200){
                List<Admin> obj = (List<Admin>) result.getObj();
                for (Admin admin : obj) {
                    System.out.println("用户id: "+admin.getId()+"     用户名称: "+admin.getNickname()+"     用户昵称: "+admin.getUsername()+"         用户权限:  "+(admin.getPermission()==1?"最高权限":"普通权限"));
                }
            }else {
                System.out.println(result.getMsg());
            }
            break;
        }
    }

    private void insertAdmin() {
        while (true){
            System.out.println("请输入昵称");
            String nickname = scanner.next();
            System.out.println("请输入用户名");
            String username = scanner.next();
            System.out.println("请输入密码");
            String password = scanner.next();
            System.out.println("请设定权限(1为最高,0为普通)");
            Integer permission = scanner.nextInt();
            Admin admin = new Admin(0, nickname, username, password, permission);
            Result result = adminService.insertAdmin(admin);
            System.out.println(result.getMsg());
            break;
        }
    }

    private void deleteAdmin() {
        while (true){
            Result result = adminService.selectAll();
            if (result.getCode()==200){
                List<Admin> obj = (List<Admin>) result.getObj();
                for (Admin admin : obj) {
                    System.out.println("用户id: "+admin.getId()+"     用户名称: "+admin.getNickname()+"     用户昵称: "+admin.getUsername()+"         用户权限:  "+(admin.getPermission()==1?"最高权限":"普通权限"));
                }
                System.out.println("请输入想要删除的管理员id");
                int id = scanner.nextInt();
                Admin admin=null;
                for (Admin admin1 : obj) {
                    if (admin1.getId()==id&&admin1.getId()!= AppData.admin.getId()){
                        admin=admin1;
                    }
                }
               if (admin!=null){
                   Result result1 = adminService.deleteAdmin(admin);
                   System.out.println(result1.getMsg());
               }
            }else {
                System.out.println(result.getMsg());
            }
            break;
        }
    }

    private void updateAdmin() {
        while (true){
            Result result = adminService.selectAll();
            if (result.getCode()==200){
                List<Admin> obj = (List<Admin>) result.getObj();
                for (Admin admin : obj) {
                    System.out.println("用户id: "+admin.getId()+"     用户名称: "+admin.getNickname()+"     用户昵称: "+admin.getUsername()+"         用户权限:  "+(admin.getPermission()==1?"最高权限":"普通权限"));
                }
                System.out.println("请输入想要修改的管理员");
                int id = scanner.nextInt();
                if (id!=AppData.admin.getId()){
                    Result result1 = adminService.selectAdminById(id);
                    if (result1.getCode()==200){
                        Admin obj1 = (Admin) result1.getObj();
                        if (obj1!=null){
                            System.out.println("请输入修改后的权限");
                            Integer i = scanner.nextInt();
                            obj1.setPermission(i);
                            Result result2 = adminService.updateAdmin(obj1);
                            System.out.println(result2);
                        }
                    }
                }else {
                    System.out.println("不能修改自己本身的权限");
                }

            }else {
                System.out.println(result.getMsg());
            }
            break;
        }
    }
}
